package Map1;

import java.util.*;
import java.util.function.BiConsumer;

public class test2 {
    public static void main(String[] args) {
//        Map<Person,String> m = new HashMap<>();
//        m.put(new Person("tkf",1),"北京");
//        m.put(new Person("tkf2",2),"北京");
//        m.put(new Person("tkf2",2),"北京");
//        System.out.println(m);
//        treeMap1();
//        treeMap2();
//        HashMap1();
//        hashMap3();
//        hashMap4();
//        hashMap5();
        treeMap2();
    }
    public static void hashMap5() {
        HashMap<String,List<String>> s = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("南京市");
        list.add("苏州市");
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("密云区");
        list2.add("怀柔区");
        s.put("江苏省",list );
        s.put("北京市",list2);
        StringBuilder sB = new StringBuilder();
        for (Map.Entry<String, List<String>> stringListEntry : s.entrySet()) {
            if(sB.length() != 0)sB.append("***");
                    sB.append(stringListEntry.getKey()).append(":");
            for (String string : stringListEntry.getValue()) {
                sB.append(string+",");
            }
        }
        System.out.println(sB);
    }
    public static void hashMap4() {
        String s = "aababcabcdabcde";
        System.out.println(Arrays.toString(s.split("")));
        HashMap<String,Integer> m =  new HashMap<>();
        for (String string :  s.split("")) {
            if(m.get(string)==null){
                m.put(string,1);
            }else{
                m.put(string,m.get(string)+1);
            }
        }
        System.out.println(m);
        StringBuilder res = new StringBuilder();
        for (Map.Entry<String, Integer> stringIntegerEntry : m.entrySet()) {
            res.append(stringIntegerEntry.getKey() + "(" + stringIntegerEntry.getValue() + ")");
        }
        System.out.println(res.toString());

    }
    public static void hashMap3() {
        HashMap<String,String> m = new HashMap<>();
        m.put("张三","北京");
        m.put("李四","北京3");
        m.put("王五","北京2");
//        m.forEach((key,value)->{
//            System.out.println(key+"---"+value);
//        });
        m.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String s, String s2) {
                System.out.println(s);
                System.out.println(s2);
            }
        });
    }
    public static void treeMap1(){
        Map<Person,String> m = new TreeMap<>(); // 是按照key排序,但是自定义对象不能排序
        m.put(new Person("tkf",1),"北京");
        m.put(new Person("tkf2",2),"北京");
        m.put(new Person("tkf2",2),"北京");
        System.out.println(m);
    }
    public static void treeMap2(){
        Map<Person,String> m = new LinkedHashMap<>(); // 是按照key排序,但是自定义对象不能排序
        m.put(new Person("tkf",1),"北京");
        m.put(new Person("tkf4",4),"北京");
        m.put(new Person("tkf4",4),"北京"); // 会对person的值完整比对, name和age都会比对.
        m.put(new Person("tkf3",3),"北京");
        m.put(new Person("tkf2",2),"北京");

        System.out.println(m);
    }
    public static void HashMap1(){
        HashMap<String,String> m = new HashMap<>();
        m.put("张三","北京");
        m.put("李四","北京3");
        m.put("王五","北京2");
        System.out.println(m.keySet());
        for (String s : m.keySet()) {
            System.out.println(m.get(s));
        }
    }
    public static void HashMap2(){
        HashMap<String,String> m = new HashMap<>();
        m.put("张三","北京");
        m.put("李四","北京3");
        m.put("王五","北京2");
        System.out.println(m.entrySet());
        Set<Map.Entry<String,String>> mE = m.entrySet(); // 每个对象都是Set里的一个对象
        for (Map.Entry<String, String> stringStringEntry : mE) {
            System.out.println(stringStringEntry);
            System.out.println(stringStringEntry.getKey());
            System.out.println(stringStringEntry.getValue());
        }
//        for (Map.Entry<String, String> stringStringEntry : m.entrySet()) {
//            System.out.println(stringStringEntry);
//            System.out.println(stringStringEntry.getKey());
//            System.out.println(stringStringEntry.getValue());
//        }

    }

}
